package com.lunwen.lunwen.services.impl;

import com.lunwen.lunwen.bean.ThesisTask;
import com.lunwen.lunwen.bean.ThesisTaskLog;
import com.lunwen.lunwen.mapper.ThesisTaskLogMapper;
import com.lunwen.lunwen.mapper.ThesisTaskMapper;
import com.lunwen.lunwen.services.ThesisTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (ThesisTask)表服务实现类
 *
 * @author makejava
 * @since 2020-08-11 09:25:35
 */
@Service
public class ThesisTaskServiceImpl implements ThesisTaskService {

    @Autowired
    private ThesisTaskMapper thesisTaskMapper;

    @Autowired
    private ThesisTaskLogMapper thesisTaskLogMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param thesistaskId 主键
     * @return 实例对象
     */
    @Override
    public ThesisTask queryById(String thesistaskId) {
        return this.thesisTaskMapper.queryById(thesistaskId);
    }






    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ThesisTask> queryAllByLimit(int offset, int limit) {
        return this.thesisTaskMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param thesisTask 实例对象
     * @return 实例对象
     */
    @Override
    public ThesisTask insert(ThesisTask thesisTask) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        thesisTask.setUptime(df.format(new Date()));
        if(thesisTaskMapper.checkthesis(thesisTask).size()<1)
            this.thesisTaskMapper.insert(thesisTask);
        else
            this.thesisTaskMapper.updateinserttask(thesisTask);

        return thesisTask;
    }

    /**
     * 修改数据
     *
     * @param thesisTask 实例对象
     * @return 实例对象
     */
    @Override
    public ThesisTask update(ThesisTask thesisTask) {
        this.thesisTaskMapper.update(thesisTask);
        return this.queryById(thesisTask.getThesistaskId());
    }

    /**
     * 通过主键删除数据
     *
     * @param thesistaskId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String thesistaskId) {
        return this.thesisTaskMapper.deleteById(thesistaskId) > 0;
    }



    @Override
    public List<ThesisTask> checkthesis(ThesisTask thesisTask) {

            return this.thesisTaskMapper.checkthesis(thesisTask);

        }

    @Override
    public int updateinserttask(ThesisTask thesisTask) {
        this.thesisTaskMapper.updateinserttask(thesisTask);
        return 0;

    }

    @Override
    public ThesisTask thesistaskcheck() {
        return thesisTaskMapper.thesistaskcheck();
    }

    @Override
    public int updateovertask(ThesisTask thesisTask) {
        this.thesisTaskMapper.updateovertask(thesisTask);
        ThesisTaskLog thesisTasklog=new ThesisTaskLog();
        thesisTasklog.setMessage(thesisTask.getMessage());
        thesisTasklog.setMubanId(thesisTask.getMubanId());
        thesisTasklog.setOutfileurl(thesisTask.getOutfileurl());
        thesisTasklog.setOvertime(thesisTask.getOvertime());
        thesisTasklog.setServerip(thesisTask.getServerip());
        thesisTasklog.setStatus(thesisTask.getStatus());
        thesisTasklog.setTaskname(thesisTask.getTaskname());
        thesisTasklog.setTasktype(thesisTask.getTasktype());
        thesisTasklog.setXueshengId(thesisTask.getXueshengId());
        System.out.println(thesisTasklog.getMessage());
        thesisTaskLogMapper.insert(thesisTasklog);

        return 0;
    }
}